package com.tsmti.webservice.database;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.*;

import java.util.List;
import java.util.Map;

/**
 * @Package：com.tsmti.webservice.database
 * @Class：DynamicDBDao
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/7/9 17:31
 * @Company:
 * @Version：
 * @Modified By:
 */
public class DynamicDbDao {
    private JdbcTemplate jdbcTemplate;

    public DynamicDbDao() {

    }

    public DynamicDbDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     *
     * @title: getJdbcTemplate
     * @description
     * @return
     * @return: JdbcTemplate
     */
    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void initJdbcTemplate(BasicDataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public <T> T execute(String sql, PreparedStatementCallback<T> action) throws DataAccessException {
        return jdbcTemplate.execute(sql,action);
    }

    public <T> T execute(PreparedStatementCreator psc, PreparedStatementCallback<T> action) throws DataAccessException {
        return jdbcTemplate.execute(psc,action);
    }

    public <T> List<T> query(String sql, RowMapper<T> rowMapper) throws DataAccessException {
        return  jdbcTemplate.query(sql,rowMapper);
    }

    public <T> T query(final String sql, final ResultSetExtractor<T> rse) throws DataAccessException {
        return  jdbcTemplate.query(sql,rse);
    }

    public <T> T query(String sql, Object[] args, ResultSetExtractor<T> rse) throws DataAccessException {
        return jdbcTemplate.query(sql, args, rse);
    }

    public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcTemplate.query(sql, args, rowMapper);
    }

    public <T> List<T> query(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper) throws DataAccessException {
        return jdbcTemplate.query(sql, args, argTypes,rowMapper);
    }

    public List<Map<String, Object>> queryList(String sql, Object... param) {
        List<Map<String, Object>> list;
        if (ArrayUtils.isEmpty(param)) {
            list = jdbcTemplate.queryForList(sql);
        } else {
            list = jdbcTemplate.queryForList(sql, param);
        }
        return list;
    }

    public <T> List<T> queryList(String sql, Class<T> clazz, Object... param) {
        List<T> list;

        if (ArrayUtils.isEmpty(param)) {
            list = jdbcTemplate.queryForList(sql, clazz);
        } else {
            list = jdbcTemplate.queryForList(sql, clazz, param);
        }
        return list;
    }
    public <T> T execute(String sql, CallableStatementCallback<T> action) throws DataAccessException {
        return jdbcTemplate.execute(sql,action);
    }
    public int update(String sql, PreparedStatementSetter pss){
        return jdbcTemplate.update(sql,pss);
    }

    public int update(String sql, Object... args){
        return jdbcTemplate.update(sql,args);
    }

    public <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T> action){
        return jdbcTemplate.execute(csc,action);
    }

    public Map<String, Object> queryForObject(String sql, RowMapper<T> rowMapper, Object... args) {
        return  jdbcTemplate.queryForMap(sql,rowMapper,args);
    }

    public <T> T queryForObject(String sql, Class<T> requiredType){
        return  jdbcTemplate.queryForObject(sql,requiredType);
    }

    public <T> List<T> query(String sql, RowMapper<T> rowMapper, Object... args){
        return (List<T>) jdbcTemplate.queryForMap(sql,rowMapper,args);
    }
}
